
#include "rampdowntimer.h"

int grnFlip=0;
unsigned long rampPeriod;

void initTimer3(void)
{
	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER3);
	//set timer period: 40MHz/10/2 = 2MHz = 0.5us
	//for 1000us = 1ms use Div by 0.1 and then 4, 40MHz / 0.1 / 4 = 1ms
	ROM_TimerConfigure(TIMER3_BASE, TIMER_CFG_PERIODIC);
	rampPeriod = ROM_SysCtlClockGet()/2;
	ROM_TimerLoadSet(TIMER3_BASE, TIMER_A, rampPeriod - 1);

	//Timer interrupt setup
	ROM_TimerIntClear(TIMER3_BASE, TIMER_TIMA_TIMEOUT);
	ROM_TimerIntEnable(TIMER3_BASE, TIMER_TIMA_TIMEOUT);
	ROM_IntEnable(INT_TIMER3A);
	ROM_TimerEnable(TIMER3_BASE, TIMER_A);
	bt_UARTsend("Init Timer3");
	loading_bar(24);
	load_okfail(1);

	g_ramp_counter = rampdown_time_common;
}

/*void timer3Handler(void)
{
	//reset counter value back to ulPeriod
	ROM_TimerLoadSet(TIMER3_BASE, TIMER_A, rampPeriod-1);

	//clear interrupt
	ROM_TimerIntClear(TIMER3_BASE, TIMER_TIMA_TIMEOUT);

	//bt_UARTsend("timer\n");
	grnFlip ^= 0xff;
	ROM_GPIOPinWrite(GPIO_PORTF_BASE, grnled, grnFlip);

	//fire off adc interrupt for temperature reading
	//ADCProcessorTrigger(ADC0_BASE, 0);
}*/

void timer3Handler(void)
{
	//reset counter value back to ulPeriod
	ROM_TimerLoadSet(TIMER3_BASE, TIMER_A, rampPeriod-1);

	//clear interrupt
	ROM_TimerIntClear(TIMER3_BASE, TIMER_TIMA_TIMEOUT);

	if(rampdown_condition == 1)
	{
		g_ramp_counter--;
		if ((g_ramp_counter % 2) == 0)
			{
				int temp;
				temp = g_ramp_counter / 2;
				bt_UARTsend(itoa(temp,10));
				bt_UARTsend("\r\n");
				if(speak_on)
				{
					switch(temp)
					{
					case 5: SYN_Say((unsigned char *)Volume"5"); break;
					case 4: SYN_Say((unsigned char *)Volume"4"); break;
					case 3: SYN_Say((unsigned char *)Volume"3"); break;
					case 2: SYN_Say((unsigned char *)Volume"2"); break;
					case 1: SYN_Say((unsigned char *)Volume"1"); break;
					default: break;
					}
				}
			}
	}


	if(g_ramp_counter <= 0)
	{
		bt_UARTsend("\r\nStopping system\r\n");
		if(speak_on) SYN_Say((unsigned char *)Volume"msgg 再見"); //goodbye
		belt_driver(0);
		ROM_TimerDisable(TIMER3_BASE, TIMER_A);
	}

	grnFlip ^= 0xff;
	ROM_GPIOPinWrite(GPIO_PORTF_BASE, grnled, grnFlip);
}
